﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.Types;
using MyFloridaHouse.DataAccess.DTO;
using MyFloridaHouse.DataAccess.Extensions;
using System.Linq;
using System.Data.SqlTypes;
using MyFloridaHouse.libSvDTO;
using MyFloridaHouse.DataAccess.SQLServerTypes;

namespace MyFloridaHouse.DataAccess
{

    public partial class DAL_CensusData
    {
        public List<int> GetShapeRegionIDs(int iLayerID, MapMultiShapeDTO mapShape)
        {
            SqlGeometry shapeGeom = TOOLS_SQLGeometry.BuildShape(mapShape);
            string tableName = DAL_Global.GetRegionTableNameFromLayerID(iLayerID);

            List<int> itemList = new List<int>();
            string storedProcedureName = "flh_GetShapeRegionIDs";
            using (SqlConnection db = Connect())
            {
                SqlCommand StoredProcedureCommand = new SqlCommand(storedProcedureName, db);
                StoredProcedureCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter dataPartIDParm = StoredProcedureCommand.Parameters.AddWithValue("sqlGeom", shapeGeom);
                dataPartIDParm.Direction = ParameterDirection.Input;
                dataPartIDParm.SqlDbType = SqlDbType.Udt;
                dataPartIDParm.UdtTypeName = "GEOMETRY";
                //dataPartIDParm.SourceVersion = DataRowVersion.Current; 

                SqlParameter dataPartIDParm2 = StoredProcedureCommand.Parameters.AddWithValue("tableName", tableName);
                dataPartIDParm2.Direction = ParameterDirection.Input;

                db.Open();
                using (SqlDataReader rdr = StoredProcedureCommand.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        itemList.Add((int)rdr["regionID"]);
                    }
                }
            }

            return itemList;
        }

    }
}
